﻿SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

GO
ALTER TABLE dbo.bht_ASyncMailStock
	DROP CONSTRAINT DF_bht_ASyncMailStock_SetTime
GO
CREATE TABLE dbo.Tmp_bht_ASyncMailStock
	(
	RequestId int NOT NULL IDENTITY (1, 1),
	TemplateId int NOT NULL,
	Object nvarchar(MAX) NOT NULL,
	Type nvarchar(400) NOT NULL,
	Receiver nvarchar(100) NOT NULL,
	SetTime datetime NOT NULL,
	SendTime datetime NULL,
	MessageTitle nvarchar(400) NOT NULL
	)  ON [PRIMARY]
	 TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_bht_ASyncMailStock SET (LOCK_ESCALATION = TABLE)
GO
ALTER TABLE dbo.Tmp_bht_ASyncMailStock ADD CONSTRAINT
	DF_bht_ASyncMailStock_SetTime DEFAULT (getdate()) FOR SetTime
GO
SET IDENTITY_INSERT dbo.Tmp_bht_ASyncMailStock ON
GO
IF EXISTS(SELECT * FROM dbo.bht_ASyncMailStock)
	 EXEC('INSERT INTO dbo.Tmp_bht_ASyncMailStock (RequestId, TemplateId, Object, Type, Receiver, SetTime, SendTime)
		SELECT RequestId, TemplateId, Object, Type, Receiver, SetTime, SendTime FROM dbo.bht_ASyncMailStock WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_bht_ASyncMailStock OFF
GO
DROP TABLE dbo.bht_ASyncMailStock
GO
EXECUTE sp_rename N'dbo.Tmp_bht_ASyncMailStock', N'bht_ASyncMailStock', 'OBJECT' 
GO
ALTER TABLE dbo.bht_ASyncMailStock ADD CONSTRAINT
	PK_bht_ASyncMailStock PRIMARY KEY CLUSTERED 
	(
	RequestId
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO

ALTER PROCEDURE [dbo].[bhp_ASyncMailStockSave]
	@TemplateId		INT,
	@Object			NVARCHAR(MAX),
	@Type			NVARCHAR(400),
	@Receiver		NVARCHAR(100),
	@MessageTitle	NVARCHAR(400)
AS
BEGIN

	SET NOCOUNT ON;
	
	INSERT INTO bht_ASyncMailStock (TemplateId, [Object], [Type], Receiver, MessageTitle)
		VALUES (@TemplateId, @Object, @Type, @Receiver, @MessageTitle)
		
	SELECT SCOPE_IDENTITY() as RequestId

END

GO
